package kgc.util;

import kgc.pojo.News_Category;
import kgc.pojo.News_Comment;
import kgc.pojo.News_Detail;
import kgc.pojo.News_User;

import java.sql.*;

public class BaseDao {
    static Connection connection = null;
    static Statement statement = null;
    static PreparedStatement preparedStatement = null;
    static ResultSet resultSet = null;

    public static Connection getConnection(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url ="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String user="root";
            String pass="123456";
            connection  = DriverManager.getConnection(url,user,pass);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();

        }
        return connection;
    }



    //通用查询
    public int updateObj(String sql, Object[] obj){
        int rows = 0;
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                for (int i=0;i<obj.length;i++){
                    preparedStatement.setObject(i+1,obj[i]);
                }
                rows = preparedStatement.executeUpdate();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }

    //通用查询
    public ResultSet selectObj(String sql, Object[] obj){
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                for (int i=0;i<obj.length;i++){
                    preparedStatement.setObject(i+1,obj[i]);
                }
                resultSet = preparedStatement.executeQuery();
            }else {
                resultSet = preparedStatement.executeQuery();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }


    //News_Category查询
    public ResultSet selectCategory(String sql, Object[] obj){
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_Category category = (News_Category) obj[0];
                preparedStatement.setObject(1,category.getId());
                resultSet = preparedStatement.executeQuery();
            }else {
                resultSet = preparedStatement.executeQuery();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

    //News_Comment查询
    public static ResultSet selectComment(String sql, Object[] obj){
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_Comment comment = (News_Comment) obj[0];
                preparedStatement.setObject(1,comment.getId());
                resultSet = preparedStatement.executeQuery();
            }else {
                resultSet = preparedStatement.executeQuery();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

    //News_Detail查询
    public static ResultSet selectDetail(String sql, Object[] obj){
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_Detail detail = (News_Detail) obj[0];
                preparedStatement.setObject(1,detail.getId());
                resultSet = preparedStatement.executeQuery();
            }else {
                resultSet = preparedStatement.executeQuery();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

    //News_User查询
    public ResultSet selectUser(String sql, Object[] obj){
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_User user = (News_User) obj[0];
                preparedStatement.setObject(1,user.getId());
                resultSet = preparedStatement.executeQuery();
            }else {
                resultSet = preparedStatement.executeQuery();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

    //News_Category改
    public int updateCategory(String sql, Object[] obj){
        int rows = 0;
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_Category category = (News_Category) obj[0];
                preparedStatement.setObject(1,category.getName());
                preparedStatement.setObject(2,category.getDate());
                preparedStatement.setObject(3,category.getId());
                rows = preparedStatement.executeUpdate();
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return rows;
    }

    public int updateUser(String sql, Object[] obj){
        int rows = 0;
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_User user = (News_User) obj[0];
                preparedStatement.setObject(1,user.getUserName());
                preparedStatement.setObject(2,user.getPassword());
                preparedStatement.setObject(3,user.getEmail());
                preparedStatement.setObject(4,user.getUserType());
                preparedStatement.setObject(5,user.getId());
                rows = preparedStatement.executeUpdate();
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return rows;
    }


    //News_Category增
    public int addCategory(String sql, Object[] obj){
        int rows = 0;
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_Category category = (News_Category) obj[0];
                preparedStatement.setObject(1,category.getId());
                preparedStatement.setObject(2,category.getName());
                preparedStatement.setObject(3,category.getDate());
                rows = preparedStatement.executeUpdate();
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return rows;
    }


    //News_User增
    public int addUser(String sql, Object[] obj){
        int rows = 0;
        try {
            connection = BaseDao.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (obj != null){
                News_User user = (News_User) obj[0];
                preparedStatement.setObject(1,user.getId());
                preparedStatement.setObject(2,user.getUserName());
                preparedStatement.setObject(3,user.getPassword());
                preparedStatement.setObject(4,user.getEmail());
                preparedStatement.setObject(5,user.getUserType());
                rows = preparedStatement.executeUpdate();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return rows;
    }



    //关闭连接
    public void closeConnection(ResultSet resultSet,
                                PreparedStatement preparedStatement,
                                Statement statement,
                                Connection connection){
        try {
            if (resultSet != null){
                resultSet.close();
            }
            if (preparedStatement != null){
                preparedStatement.close();
            }
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void closeConnection(PreparedStatement preparedStatement,
                                Statement statement,
                                Connection connection){
        try {
            if (preparedStatement != null){
                preparedStatement.close();
            }
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void closeConnection(ResultSet resultSet){
        try {
            if (resultSet != null){
                resultSet.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
